/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Produccion;
import Controlador.Bean.Produccion.ProductoF;
import java.util.List;
import gui.General.Main;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Nadhia
 */
public class detalleProductoBuscar extends javax.swing.JPanel {
    private int clic=0;

    /**
     * Creates new form detalleProductoBuscar
     */
    private TableModel tb;
//    private int clic=0;
    String selectedId;
    AjusteReceta ar;
    public detalleProductoBuscar(AjusteReceta ar) {
        initComponents();
        initComp();
        this.ar = ar;
    }
     private void initComp(){
         tb = new TableModel();
         tblProductos.setModel(tb);
         tblProductos.setRowSelectionAllowed(true);
         tblProductos.setDefaultRenderer(JButton.class, new MiRender());
         tblProductos.setDefaultEditor( JButton.class, new MiEditor());
     }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblProductos = new javax.swing.JTable();

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        tblProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblProductos.setMinimumSize(new java.awt.Dimension(60, 20));
        tblProductos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblProductosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblProductos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(233, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    class TableModel extends AbstractTableModel{
        
        //Two arrays used for the table data
        String[] columnNames = {"Código", "Nombre", "Cantidad", "Unidad Métrica", "Seleccionar"};
               
        public List<ProductoF> data = Controlador.Service.Produccion.GestionProduccion.obtenerProductosI();
        
        

        
        @Override
        public int getRowCount()
        {
            return data.size();
        }
        
        
        
        @Override
        public int getColumnCount()            
        {
            return columnNames.length;
        }
        
        @Override
        public Object getValueAt(int row, int column)
        {   
            switch(column){
                case 0: return data.get(row).getIdProducto();
                case 1: return data.get(row).getNombre(); 
                case 2: return data.get(row).getCantidad();
                case 3: return data.get(row).getUnidadMetrica();
                default: return new JButton("Seleccionar");
            }
        }
        
        //Used by the JTable object to set the column names
        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }
        
        //Used by the JTable object to render different
        //functionality based on the data type
        @Override
        public Class getColumnClass(int c) {
            if(c!=7){
            return getValueAt(0, c).getClass();
            }else{
                return JButton.class;
            }
        }
        
        @Override
        public boolean isCellEditable(int row, int column)
        {
	        return false;
        }
        
     }
    public class MiRender implements TableCellRenderer {
        // Este metodo indica como debe de pintarse el elemento en la fila row, en la columna column
        // que esta en la tabla table y tiene el color dado por el objeto.
        @Override
        public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) {    
            // como el boton y el calendario son componentes esto se vale.
            return (JComponent)table.getValueAt(row, column);
        }
     }
     
     public class MiEditor extends AbstractCellEditor implements TableCellEditor {
         private Boolean currentValue;
         @Override
         public Object getCellEditorValue()
         {
            return currentValue;
         }
         //El editor usara el propio componente. Para que funcione la celda en el modelo debe ser editable.
         @Override
         public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
         {
           return (JComponent) value;// la tabla solo debe tener componentes graficos
         }
     }
    
    
    private void tblProductosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblProductosMouseClicked
        // TODO add your handling code here:

        int selectedRowIndex = tblProductos.getSelectedRow();
        int selectedColumnIndex = tblProductos.getSelectedColumn();
        if(selectedColumnIndex!=4){
            String selectId = (String) tblProductos.getValueAt(selectedRowIndex, 0);
            if(clic == 1 && selectedId.compareTo(selectId)==0){
                //                txtIdProducto.setText((String)tblProductos.getValueAt(selectedRowIndex, 0));
                //                txtNombre.setText((String)tblProductos.getValueAt(selectedRowIndex, 1));
                //                txtDescripcion.setText((String)tblProductos.getValueAt(selectedRowIndex, 2));
                //                txtStockMinimo.setText(((Double)tblProductos.getValueAt(selectedRowIndex, 3)).toString());
                //                txtCantidad.setText(((Double)tblProductos.getValueAt(selectedRowIndex, 4)).toString());
                //                cmbTipo.setSelectedItem(tblProductos.getValueAt(selectedRowIndex, 5));
                //                cmbTipo.setSelectedIndex(Integer.parseInt((String)tblProductos.getValueAt(selectedRowIndex, 6)));
                clic = 0;
                //                cmbTipo.setEnabled(false);
            }else
            if(clic == 0){
                selectedId = selectId;
                clic++;
            }else{
                clic = 0;
            }
        }else{
            //            agregarMasa  am = new agregarMasa((String)tblProductos.getValueAt(selectedRowIndex, 0));

            int confirmado = JOptionPane.showConfirmDialog(this,"¿Desea seleccionar el producto?");
            if (confirmado == JOptionPane.OK_OPTION){
                agregarProducto  ap = new agregarProducto((String)tblProductos.getValueAt(selectedRowIndex, 0),(String)tblProductos.getValueAt(selectedRowIndex, 1));
                Main.getDesktop().add(ap);
                ap.setVisible(true);
//                this.dispose();
                //                Controlador.Service.Almacen.GestionAlmacenes.eliminarProductos((String)tblProductos.getValueAt(selectedRowIndex, 0));
                //                tb.actualizarTabla();
            }
        }
    }//GEN-LAST:event_tblProductosMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblProductos;
    // End of variables declaration//GEN-END:variables
}
